package com.tanhua.server.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.service.UserFreezeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author lifeihu
 * @date 2022/1/5
 * @TIME:18:42
 */
@Service
public class UserFreezeServiceImpl implements UserFreezeService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void checkUserStatus(Integer state, Long userId) {
        //1.查询redis校验用户是否冻结
        String value = (String) redisTemplate.opsForValue().get(Constants.FREEZE_USER + userId);
        if(!StringUtils.isEmpty(value)){
            Map map = JSON.parseObject(value,Map.class);
            String freezingRange1 = (String) map.get("freezingRange");
            Integer freezingRange = Integer.parseInt(freezingRange1);
            if(freezingRange==state){
                throw new BusinessException(ErrorResult.builder().errMessage("您的账号被冻结！").build());
            }
        }
    }
}
